<?php

namespace App\Http\Resources;

use App\Enums\WorkTicketStatusEnum;
use App\Enums\WorkTicketTransferStatusEnum;
use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\JsonResource;

/**
 * @OA\Schema(
 *     schema="WorkTicketResource",
 *     title="工单资源",
 *     description="工单信息",
 *     @OA\Property(property="id", type="integer", description="工单ID", example=1),
 *     @OA\Property(property="code", type="string", description="工单编号", example="WT202501010001"),
 *     @OA\Property(property="school_id", type="integer", description="学校ID", example=1),
 *     @OA\Property(property="school_user_id", type="integer", description="学校用户ID", example=1),
 *     @OA\Property(property="room_number", type="string", description="房间号", example="A101"),
 *     @OA\Property(property="contact_name", type="string", description="联系人姓名", example="张三"),
 *     @OA\Property(property="contact_tel", type="string", description="联系人电话", example="13800138000"),
 *     @OA\Property(property="content", type="string", description="工单内容", example="空调不制冷，需要维修"),
 *     @OA\Property(property="work_user_id", type="integer", description="维护人员ID", example=1),
 *     @OA\Property(property="status", type="integer", description="工单状态", example=100),
 *     @OA\Property(property="status_text", type="string", description="工单状态文本", example="待处理"),
 *     @OA\Property(property="finish_at", type="string", format="date-time", description="完成时间", example="2025-01-01 15:30:00"),
 *     @OA\Property(property="created_at", type="string", format="date-time", description="创建时间", example="2025-01-01 12:00:00"),
 *     @OA\Property(property="updated_at", type="string", format="date-time", description="更新时间", example="2025-01-01 12:00:00"),
 *     @OA\Property(property="school", type="object", description="关联的学校信息", ref="#/components/schemas/SchoolResource"),
 *     @OA\Property(property="school_user", type="object", description="关联的学校用户信息", ref="#/components/schemas/SchoolUserResource"),
 *     @OA\Property(property="work_user", type="object", description="关联的维护人员信息", ref="#/components/schemas/WorkUserResource"),
 *     @OA\Property(property="details", type="array", description="工单详情列表", @OA\Items(ref="#/components/schemas/WorkTicketDetailResource")),
 *     @OA\Property(property="transfers", type="array", description="工单转接记录", @OA\Items(ref="#/components/schemas/WorkTicketTransferResource")),
 *     @OA\Property(property="rating", type="object", description="工单评价", ref="#/components/schemas/WorkTicketRatingResource"),
 *     @OA\Property(property="is_transfer", type="boolean", description="是否转接")
 * )
 * @mixin \App\Models\WorkTicket
 */
class WorkTicketResource extends JsonResource
{
    /**
     * Transform the resource into an array.
     *
     * @return array<string, mixed>
     */
    public function toArray(Request $request): array
    {
        return [
            'id' => $this->id,
            'code' => $this->code,
            'school_id' => $this->school_id,
            'school_name' => $this->school?->name,
            'school_user_id' => $this->school_user_id,
            'school_user_name' => $this->schoolUser?->real_name,
            'room_number' => $this->room_number,
            'contact_name' => $this->contact_name,
            'contact_tel' => $this->contact_tel,
            'content' => $this->content,
            'work_user_id' => $this->work_user_id,
            'work_user_name' => $this->workUser?->real_name,
            'status' => $this->status,
            'status_text' => WorkTicketStatusEnum::getKey($this->status),
            'finish_at' => $this->finish_at?->toDateTimeString(),
            'created_at' => $this->created_at?->toDateTimeString(),
            'updated_at' => $this->updated_at?->toDateTimeString(),
            'school' => new SchoolResource($this->whenLoaded('school')),
            'school_user' => new SchoolUserResource($this->whenLoaded('schoolUser')),
            'work_user' => new WorkUserResource($this->whenLoaded('workUser')),
            'details' => WorkTicketDetailResource::collection($this->whenLoaded('details')),
            // 'images' => $this->whenLoaded('images', function () {
            //     return $this->images->pluck('path');
            // }),
            'transfers' => WorkTicketTransferResource::collection($this->whenLoaded('transfers')),
            'rating' => new WorkTicketRatingResource($this->whenLoaded('rating')),

            'is_transfer' => $this->transfers->where('status', WorkTicketTransferStatusEnum::待接受)->isNotEmpty(),
        ];
    }
} 